import * as types from "../mutation-types";

const opened =
  localStorage.getItem("sidebarStatus") &&
  localStorage.getItem("sidebarStatus") === "true"
    ? true
    : false;

const app = {
  state: {
    language: localStorage.getItem("language") || "zh",
    sidebar: {
      opened
    }
  },
  mutations: {
    [types.SET_LANGUAGE]: (state, language) => {
      state.language = language;
      localStorage.setItem("language", language);
    },
    [types.TOGGLE_SIDEBAR]: state => {
      state.sidebar.opened = !state.sidebar.opened;
      localStorage.setItem("sidebarStatus", state.sidebar.opened);
    }
  },
  actions: {
    SetLanguage({ commit }, language) {
      commit(types.SET_LANGUAGE, language);
    },
    ToggleSideBar: ({ commit }) => {
      commit(types.TOGGLE_SIDEBAR);
    }
  }
};

export default app;
